﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DTTB.Common;
using DTTB.MV2.QUANLYNHANSUTIENLUONG.Models.FormBean.NhanVienModel;
using DTTB.MV2.QUANLYNHANSUTIENLUONG.Models.LinQ.NhanVien;
using Webdiyer.WebControls.Mvc;

namespace DTTB.MV2.QUANLYNHANSUTIENLUONG.BussinessDelegate.NhanVienDelegate
{
    public class TrachNhiemDelegate
    {
        private NhanVienDataContext db = new NhanVienDataContext();
        int _UserId = Convert.ToInt32(HttpContext.Current.Session["USERID"]);
        int _DeparmentId = Convert.ToInt32(HttpContext.Current.Session["DEPARTMENT"]);
        DateTime _DateNow = DateTime.Now;
        public IList<hs_NhanVien_TrachNhiem> GetAllTrachNhiem()
        {
            return db.hs_NhanVien_TrachNhiems.Where(p => p.IsDelete == false).ToList();
        }
        public bool UpdateTrangThaiSuDung(int Id)
        {
            try
            {
                hs_NhanVien_TrachNhiem hs = db.hs_NhanVien_TrachNhiems.Single(p => p.Id == Id);
                hs.TrangThaiSuDung = true;
                hs.UpdateDate = DateTime.Now;
                hs.UpdateBy = Convert.ToInt32(HttpContext.Current.Session["USERID"]);
                db.SubmitChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
        public bool UpdateTrangThaiKhongSuDung(int IdNhanVien, int Id)
        {
            try
            {
                List<hs_NhanVien_TrachNhiem> hs = db.hs_NhanVien_TrachNhiems.Where(p => p.Id != Id&&p.IdNhanVien==IdNhanVien).ToList();
                foreach (var hsNhanVienPCTrachNhiem in hs)
                {
                    hsNhanVienPCTrachNhiem.TrangThaiSuDung = false;
                    db.SubmitChanges();
                }

                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
        public hs_NhanVien_TrachNhiem Insert(hs_NhanVien_TrachNhiem obj)
        {
            try
            {

                obj.CreateBy = _UserId;
                obj.CreateDate = _DateNow;
                db.hs_NhanVien_TrachNhiems.InsertOnSubmit(obj);
                db.SubmitChanges();
                return obj;
            }
            catch (Exception)
            {
                return null;
            }
        }
        public hs_NhanVien_TrachNhiem AddNewTrachNhiem(hs_NhanVien_TrachNhiem Luong)
        {
            db.hs_NhanVien_TrachNhiems.InsertOnSubmit(Luong);
            db.SubmitChanges();
            return GetLoaiCongChucIdMax();
        }

        public bool DeleteTrachNhiem(int ID)
        {
            try
            {
                hs_NhanVien_TrachNhiem LuongNew =
               db.hs_NhanVien_TrachNhiems.Single(p => p.Id == ID);

                db.hs_NhanVien_TrachNhiems.DeleteOnSubmit(LuongNew);
                db.SubmitChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }



        public bool UpdateTrachNhiem(hs_NhanVien_TrachNhiem TrachNhiem)
        {
            try
            {
                hs_NhanVien_TrachNhiem TrachNhiemUpdate =
                    db.hs_NhanVien_TrachNhiems.Single(p => p.Id == TrachNhiem.Id);
                if (TrachNhiemUpdate != null && TrachNhiemUpdate.IsDelete == false)
                {
                    TrachNhiemUpdate.Id = TrachNhiem.Id;
                    TrachNhiemUpdate.IdTrachNhiem = TrachNhiem.IdTrachNhiem;
                    TrachNhiemUpdate.UpdateDate = TrachNhiem.UpdateDate;
                    TrachNhiemUpdate.UpdateBy = TrachNhiem.UpdateBy;
                    db.SubmitChanges();
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch(Exception)
            {
                return false;
            }
        }



        public hs_NhanVien_TrachNhiem GetTrachNhiemById(string id)
        {
            try
            {
                hs_NhanVien_TrachNhiem item = this.db.hs_NhanVien_TrachNhiems.Single(p => p.Id == Convert.ToInt32(id));
                return item;
            }
            catch (Exception)
            {
                return null;
            }
        }


        public hs_NhanVien_TrachNhiem BuildnewTrachNhiem(TrachNhiemForm form)
        {
            hs_NhanVien_TrachNhiem newTrachNhiem = new hs_NhanVien_TrachNhiem();
            newTrachNhiem.IdTrachNhiem = form.IdTrachNhiem;
            newTrachNhiem.NgayThayDoi = form.NgayThayDoi;
     
            newTrachNhiem.CreateBy = Convert.ToInt32(HttpContext.Current.Session["USERID"]);
            newTrachNhiem.CreateDate = DateTime.Now;
            newTrachNhiem.IsDelete = false;
            return newTrachNhiem;
        }


        public hs_NhanVien_TrachNhiem BuildEditNhanVien(TrachNhiemForm form)
        {
            hs_NhanVien_TrachNhiem newTrachNhiem = new hs_NhanVien_TrachNhiem();
            newTrachNhiem.Id = form.Id;
            newTrachNhiem.IdTrachNhiem = form.IdTrachNhiem;
            newTrachNhiem.NgayThayDoi = form.NgayThayDoi;

            newTrachNhiem.UpdateDate = DateTime.Now;
            newTrachNhiem.UpdateBy = Convert.ToInt32(HttpContext.Current.Session["USERID"]);
            return newTrachNhiem;
        }

        public hs_NhanVien_TrachNhiem GetLoaiCongChucIdMax()
        {
            IList<hs_NhanVien_TrachNhiem> list = db.hs_NhanVien_TrachNhiems.ToList();
            return list.Count != 0 ? list.ElementAt(list.Count - 1) : null;
        }

        public TrachNhiemForm ConvertToForm(hs_NhanVien_TrachNhiem obj)
        {
            TrachNhiemForm form = new TrachNhiemForm();
            form.Id = obj.Id;
            form.IdTrachNhiem = obj.IdTrachNhiem;
            form.NgayThayDoi = obj.NgayThayDoi;
            form.UpdateDate = obj.UpdateDate != null ? obj.UpdateDate.Value : DateTime.MinValue;
            form.CreateDate = obj.CreateDate != null ? obj.CreateDate.Value : DateTime.MinValue;
            form.UpdateBy = obj.UpdateBy != null ? obj.UpdateBy.Value : 0;
            form.CreateBy = obj.CreateBy != null ? obj.CreateBy.Value : 0;
            return form;
        }

        public IQueryable<hs_NhanVien_TrachNhiem> GetAll(int IdNhanVien)
        {
            return db.hs_NhanVien_TrachNhiems.Where(p => p.IsDelete == false&&p.IdNhanVien==IdNhanVien).OrderByDescending(p => p.CreateBy);
        }

        public IQueryable<hs_NhanVien_TrachNhiem> QuickSearch(int IdLuong,int? pageNumberId, string SearchObject, string SearchInput)
        {
            IQueryable<hs_NhanVien_TrachNhiem> lst = null;
            try
            {
                SearchInput = SearchInput.Trim();
                lst = this.GetAll(IdLuong);
                if (SearchObject.Equals("TrachNhiem"))
                    lst = lst.Where(p => p.dm_TrachNhiem.TienTrachNhiem.Value.ToString().Contains(SearchInput));
            }
            catch (Exception)
            {

            }
            return lst;
        }

        public SelectList BuildTrachNhiem(int IdTrachNhiem)
        {
            SelectList list = new SelectList(db.dm_TrachNhiems.Where(p => p.IsDelete == false).ToList(), "Id", "TienTrachNhiem", IdTrachNhiem);
            return list;
        }
        public hs_NhanVien_TrachNhiem ConvertToLinq(TrachNhiemForm form)
        {
            hs_NhanVien_TrachNhiem obj = new hs_NhanVien_TrachNhiem();
            obj.IdTrachNhiem = form.IdTrachNhiem;
            obj.IdNhanVien = form.IdNhanVien;
            obj.NgayThayDoi = DateTime.Now;
            return obj;
        }
    }
}